package cn.exrick.xboot.modules.base.serviceimpl.mybatis;

import cn.exrick.xboot.common.exception.ServiceException;
import cn.exrick.xboot.factory.MyBeanFactory;
import cn.exrick.xboot.modules.base.dao.mapper.PositionMapper;
import cn.exrick.xboot.modules.base.entity.Position;
import cn.exrick.xboot.modules.base.enums.OrganizationExceptionEnum;
import cn.exrick.xboot.modules.base.param.PositionParam;
import cn.exrick.xboot.modules.base.service.mybatis.IPositionService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 岗位接口实现
 * @author njp
 */
@Slf4j
@Service
@Transactional
public class IPositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements IPositionService {


    @Override
    public void add(PositionParam positionParam) {
        checkParam(positionParam,"add");
        Position pos = MyBeanFactory.getMyBeanFactory().createBean(Position.class);
        CopyOptions copyOptions=new CopyOptions();
        copyOptions.setIgnoreNullValue(true);
        //添加默认排序值
        pos.setSortOrder(new BigDecimal(0));
        BeanUtil.copyProperties(positionParam,pos,copyOptions);
        this.save(pos);
    }

    @Override
    public void edit(PositionParam positionParam) {
        //暂时只做标题的修改
        checkParam(positionParam,"edit");
        Position pos = MyBeanFactory.getMyBeanFactory().createBean(Position.class);
        CopyOptions copyOptions=new CopyOptions();
        copyOptions.setIgnoreNullValue(true);
        BeanUtil.copyProperties(positionParam,pos,copyOptions);
        this.saveOrUpdate(pos);
    }


    /**
     * 大篇幅的参数校验在这个方法中做
     * @param positionParam
     * @param methodName
     */
    private void checkParam(PositionParam positionParam,String methodName){
        switch (methodName){
            case "add":
                if(ObjectUtil.isNull(positionParam.getTitle())){
                 throw new ServiceException(OrganizationExceptionEnum.POSITION_TITLE_IS_NOTNULL);
                }
                if(ObjectUtil.isNull(positionParam.getOrgId())){
                    throw new ServiceException(OrganizationExceptionEnum.POSITION_ORGID_IS_NOTNULL);
                }
                break;
            case "edit":
                if(ObjectUtil.isNull(positionParam.getTitle())){
                    throw new ServiceException(OrganizationExceptionEnum.POSITION_TITLE_IS_NOTNULL);
                }
                break;
            default:
                break;
        }

    }
}